var request = require('request'),
	cheerio = require('cheerio'),
	baseUrl = 'http://www.benzinga.com/stock/{0}/ratings',
	ticker = process.argv[2],
	url = baseUrl.replace('{0}', ticker);

request(url, function(err, resp, body) {
	var $ = cheerio.load(body),
		$trs = $('.stock-ratings-calendar tbody tr'),
		opinions = [],
		opinionsString;
	
	$trs.each(function(i, tr){
		var $tds = $(tr).find('td'),
			opinion = {
				Date: $($tds.get(0)).text(),
				Firm: $($tds.get(1)).text(),
				Action: $($tds.get(2)).text(),
				Previous: null,
				Current: $($tds.get(3)).text(),
				PriceTarget: parseFloat($($tds.get(4)).text())
			};

		opinions.push(opinion);
	});

	opinionsString = JSON.stringify(opinions);
	console.log(opinionsString);
});